package com.example.databasehomework;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.NumberPicker;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class Activity_Student_Detail extends AppCompatActivity {

    private int seatNum;
    private String stuNum, stuName, expId;
    private TextView headStuName, headStuNum, headSeatNum;
    private ImageView returnImg;
    private RadioButton signIn_yes, signIn_no, signOut_yes, signOut_no;
    private NumberPicker passNum, ask;
    private EditText other;
    private Button sure, exit;

    private String[] askLevel = {"极好", "较好", "一般", "较差", "极差"};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_student_detail);
        Intent intent = getIntent();
        seatNum = intent.getIntExtra("seatNum", -1);
        stuName = intent.getStringExtra("stuName");
        stuNum = intent.getStringExtra("stuNum");
        expId = intent.getStringExtra("expId");
        getViews();
        setViews();
        setHeadViews();
        setButtonListener();
        getStudentInfo();
    }

    public void getViews() {
        headStuNum = findViewById(R.id.stu_info_head_stunum);
        headStuName = findViewById(R.id.stu_info_head_stuname);
        headSeatNum = findViewById(R.id.stu_info_head_seatnum);
        returnImg = findViewById(R.id.stu_info_head_img);
        signIn_yes = findViewById(R.id.stu_info_body_signin_yes);
        signIn_no = findViewById(R.id.stu_info_body_signin_no);
        signOut_yes = findViewById(R.id.stu_info_body_signout_yes);
        signOut_no = findViewById(R.id.stu_info_body_signout_no);
        passNum = findViewById(R.id.stu_info_body_testnum_np);
        ask = findViewById(R.id.stu_info_body_qa_np);
        other = findViewById(R.id.stu_info_body_other_edit);
        sure = findViewById(R.id.stu_info_button_yes);
        exit = findViewById(R.id.stu_info_button_no);
    }

    public void setViews() {
        ask.setDisplayedValues(askLevel);
        ask.setMinValue(0);
        ask.setMaxValue(askLevel.length - 1);
        passNum.setMaxValue(5);
        passNum.setMinValue(0);
    }

    @SuppressLint("SetTextI18n")
    public void setHeadViews() {
        headSeatNum.setText(Integer.toString(seatNum));
        headStuName.setText(stuName);
        headStuNum.setText("#" + stuNum);
    }

    public void setButtonListener() {
        returnImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setStudentInfo();
            }
        });
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    public void getStudentInfo() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                StudentInfo info = HttpUnit.getStudentExpInfo(expId, stuNum);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (info == null) {
                            Toast.makeText(getBaseContext(), "获取学生信息失败", Toast.LENGTH_SHORT).show();
                        } else {
                            if (info.ifSignIn) signIn_yes.setChecked(true);
                            else signIn_no.setChecked(true);
                            if (info.ifSignOut)signOut_yes.setChecked(true);
                            else signOut_no.setChecked(true);
                            passNum.setValue(info.passNum);
                            ask.setValue(info.ask);
                            other.setText(info.other);
                        }
                    }
                });
            }
        }).start();
    }

    public void setStudentInfo() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                StudentInfo info = new StudentInfo(signIn_yes.isChecked(), signOut_yes.isChecked(),
                        passNum.getValue(), ask.getValue(), other.getText().toString());
                boolean success = HttpUnit.modifyStudentExpAllInfo(expId, stuNum, info);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (success) {
                            finish();
                        } else {
                            Toast.makeText(getBaseContext(), "更改学生信息失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        }).start();
    }
}